package com.qf.czh.vhr.Controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.qf.czh.vhr.Service.PositionService;
import com.qf.czh.vhr.entity.Position;
import com.qf.czh.vhr.entity.RespBean;
import com.qf.czh.vhr.entity.RespPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
@RequestMapping("/system/basic/position") // 要按照数据库的路径/system/basic/** **可以自定义
public class PositionController {
    @Autowired
    PositionService positionService;

    @GetMapping("/")
    @ResponseBody
    public List<Position> getAllPositions() {
        List<Position> list = positionService.getAllPositions();
        return list;
    }

    @GetMapping("/page")
    @ResponseBody
    public RespPageBean getPositionByPage(@RequestParam(defaultValue = "1") Integer page , @RequestParam(defaultValue = "10") Integer size){
        RespPageBean respPageBean =positionService.getPositionByPage(page , size);
        return respPageBean;
    }

    @PostMapping("/")
    @ResponseBody
    public RespBean doAddPosition(@RequestBody Position position) {
        Integer i = positionService.addPosition(position);
        if (i == 1) {
            RespBean respBean = RespBean.ok("添加成功");
            return respBean;
        } else if (i == -1) {
            RespBean respBean = RespBean.error("职位重复");
            return respBean;
        } else {
            RespBean respBean = RespBean.error("添加失败");
            return respBean;
        }
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    public RespBean deletePosition(@PathVariable Integer id) {
        Integer i = positionService.deletePositionByid(id);
        if (i == 1) {
            RespBean respBean = RespBean.ok("删除成功");
            return respBean;
        } else {
            RespBean respBean = RespBean.error("删除失败");
            return respBean;
        }
    }

    @PutMapping("/")
    @ResponseBody
    public RespBean updatePosition(@RequestBody Position position) {
        Integer i = positionService.updatePosition(position);
        if (i == 1) {
            RespBean respBean = RespBean.ok("更新成功");
            return respBean;
        } else {
            RespBean respBean = RespBean.ok("更新失败");
            return respBean;
        }
    }

    @GetMapping("/download")
    public void downLoadExcel(HttpServletResponse response) throws IOException {
        List<Position> allPositions = positionService.getAllPositions();
        response.setHeader("Content-Disposition", "attachment;filename=" + new String("职位列表.xlsx".getBytes("UTF-8"), "ISO-8859-1"));
        EasyExcel.write(response.getOutputStream(), Position.class).sheet("职位列表").doWrite(allPositions);
    }

    @PostMapping("/import")
    @ResponseBody
    public RespBean importExcel(MultipartFile file) {
        System.out.println("importExcel");
        System.out.println("file.isEmpty() = " + file.isEmpty());
        System.out.println(file.getName());
//        System.out.println();
//        try {
//            //file.getInputStream() 请求上来的excel文件,要打开输入流
//            // Position.class  这个excel文件里的每一行转换成指定的对象
//            //上传上来之后,表里面的一条条要执行哪个后端方法,比如写入数据库.list就是多条数据组合成的list
//            EasyExcel.read(file.getInputStream() , Position.class  ,  new PageReadListener<Position>(list->{
//                positionService.addPositionBatch(list);
//            })).sheet().doRead();
//            return RespBean.ok("导入成功");
//        } catch (IOException e) {
//
//            return RespBean.error("导入失败");
//        }

        try {
            EasyExcel.read(file.getInputStream(), Position.class, new PageReadListener<Position>(list -> {
                positionService.addPositionBatch(list);
            })).sheet().doRead();
            return RespBean.ok("导入成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return RespBean.error("导入失败");
    }

    @PostMapping("/hello")
    public void hello() {
        System.out.println("hello");
    }

}
